#!/usr/bin/python
import sys
import os
import pexpect
import time
import pp
def expectandshow(child, expect):
        index=child.expect(expect)
        print child.before,child.after,
        return index
def set_etc_hosts(ip,order):
	password="passw0rd"
	cmd = "ssh root@" + ip + " " + \
                " echo \'127.0.0.1       localhost.localdomain localhost\' > /etc/hosts;"
        for aip in order:
                cmd += "echo \'"+aip+" "+aip+"\' >> /etc/hosts;"
        cmd += "/bin/hostname "+ip +";" 
	HOSTNAME=ip
        NETWORKING="yes"
        NETWORKING_IPV6="no"
        NISNAME="SecondPaaS"

        cmd += " echo \'" + "NETWORKING="+NETWORKING+"\' > /etc/sysconfig/network;" \
                + "echo \'NETWORKING_IPV6=" + NETWORKING_IPV6 + "\' >> /etc/sysconfig/network ;" \
                + "echo \'HOSTNAME=" + HOSTNAME + "\' >> /etc/sysconfig/network; "\
                + "echo \'NISDOMAIN=" + NISNAME + "\' >> /etc/sysconfig/network; "	

	print cmd
        scp_child=pexpect.spawn(cmd)
        index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+ip+"'s password:", \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
        if index == 0:
                scp_child.sendline('yes')
                ret = expectandshow(scp_child, ["root@"+ip+"'s password:", pexpect.EOF])
                if ret == 0:
                        scp_child.sendline(password)
                        expectandshow(scp_child, [pexpect.EOF])
                elif ret == 1:
                        pass            
        elif index == 1:
                scp_child.sendline(password)                    
                expectandshow(scp_child, [pexpect.EOF])         
        elif index == 2:
                print "timeout, ignore and continue."
        elif index == 3:
                pass    


def set_salve_nfs(Salve_IP,Master_IP):
        argv1="/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1"
        argv2="LABEL=/boot             /boot                   ext3    defaults        1 2"
        argv3="tmpfs                   /dev/shm                tmpfs   defaults        0 0"
        argv4="devpts                  /dev/pts                devpts  gid=5,mode=620  0 0"
        argv5="sysfs                   /sys                    sysfs   defaults        0 0"
        argv6="proc                    /proc                   proc    defaults        0 0"

        password="passw0rd"

        cmd = "ssh root@"+Salve_IP+" " + \
                "echo \'" + argv1 + "\' > /etc/fstab;" + \
                "echo \'" + argv2 + "\' >> /etc/fstab;" + \
                "echo \'" + argv3 + "\' >> /etc/fstab;" + \
                "echo \'" + argv4 + "\' >> /etc/fstab;" + \
                "echo \'" + argv5 + "\' >> /etc/fstab;" + \
                "echo \'" + argv6 + "\' >> /etc/fstab;" + \
                "echo \'"+ Master_IP+":/home /home nfs defaults 0 0\' >> /etc/fstab ;" + \
                "/sbin/chkconfig netfs off;" + \
                "/sbin/chkconfig netfs on;" + \
                "mount /home"

        print cmd
        scp_child=pexpect.spawn(cmd)
        index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+Salve_IP+"'s password:", \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
        if index == 0:
                scp_child.sendline('yes')
                ret = expectandshow(scp_child, ["root@"+Salve_IP+"'s password:", pexpect.EOF])
                if ret == 0:
                        scp_child.sendline(password)
                        expectandshow(scp_child, [pexpect.EOF])
                elif ret == 1:
                        pass
        elif index == 1:
                scp_child.sendline(password)
                expectandshow(scp_child, [pexpect.EOF])
        elif index == 2:
                print "timeout, ignore and continue."
        elif index == 3:
                pass


def set_salve_nis(ip):
        password="passw0rd"
        cmd="ssh root@"+ip+ " " + \
                "/sbin/chkconfig ypbind on;" + \
                "/etc/init.d/ypbind start;"


        print cmd
        scp_child=pexpect.spawn(cmd)
        index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+ip+"'s password:", \
                                "next host to add:", \
                                "Is this correct?  [y/n: y]" , \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
        if index == 0:
                scp_child.sendline('yes')
                ret = expectandshow(scp_child, ["root@"+Master_IP+"'s password:",pexpect.EOF])
                if ret == 0:
                        scp_child.sendline(password)
                        ret1 = expectandshow(scp_child, ["next host to add:", pexpect.EOF])
                        if ret1 == 0:
                                scp_child.sendcontrol('D')
                                ret2 = expectandshow(scp_child, ["Is this correct?  [y/n: y]",pexpect.EOF])
                                if ret2 == 0:
                                        scp_child.sendline("y")
                                        expectandshow(scp_child, [pexpect.EOF])
                                elif ret2 == 1:
                                        pass
                        elif ret1 == 1:
                                pass
                elif ret == 1:
                        pass
        elif index == 1:
                scp_child.sendline(password)
                ret1 = expectandshow(scp_child, ["next host to add:",pexpect.EOF])
                if ret1 == 0:
                        scp_child.sendcontrol('D')
                        ret2 = expectandshow(scp_child, ["Is this correct?  [y/n: y]",pexpect.EOF])
                        if ret2 == 0:
                                scp_child.sendline("y")
                                expectandshow(scp_child, [pexpect.EOF])
                        elif ret2 == 1:
                                pass
                elif ret1 == 1:
                        pass

        elif index == 2:
                print "timeout, ignore and continue."
        elif index == 3:
                pass



def set_rsh(ip,order):
        cmd="ssh root@" + ip + " echo '' > /etc/hosts.equiv;"
        for ip1 in order:
                cmd += "echo " +ip1 + " >> /etc/hosts.equiv;"
        cmd += "echo '' > /root/.rhosts;"
        for ip2 in order:
                cmd += "echo " +ip2 + " >> /root/.rhosts;"

        cmd +=  "/sbin/chkconfig rsh on;" + \
                "/etc/init.d/xinetd start;"

        print cmd
        password="passw0rd"
        scp_child=pexpect.spawn(cmd)
        index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+ip+"'s password:", \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
        if index == 0:
                scp_child.sendline('yes')
                ret = expectandshow(scp_child, ["root@"+ip+"'s password:",pexpect.EOF])
                if ret == 0:
                        scp_child.sendline(password)
                        expectandshow(scp_child, [pexpect.EOF])
                elif ret == 1:
                        pass
        elif index == 1:
                scp_child.sendline(password)
                expectandshow(scp_child, [pexpect.EOF])

        elif index == 2:
                print "timeout, ignore and continue."
        elif index == 3:
                pass


#########main#############

order=[]
path=sys.path[0]
fp_intr = open(path+'/ip','r')
total_ip = 0
while True:
    	line = fp_intr.readline()
    	length = len(line)
	if length == 0: # Zero length indicates EOF
        	break
	line=line[0:length-1] 
	total_ip += 1
	print line
	order.append(line)

fp_intr.close()
Master_IP=order[0]
#total_ip=1

password="passw0rd"
start_time_total = time.time()

#############


###Set etc_hosts ###
###Set etc_sysconfig_network ###

start_time = time.time()
ppservers = ()
job_server = pp.Server(total_ip, ppservers=ppservers)
print "Starting pp with", job_server.get_ncpus(), "workers"
jobs = []
for ip in order:
        jobs.append(job_server.submit(set_etc_hosts, (ip,order,), (expectandshow,), ("pexpect",)))

for job in jobs:
    job()

print "Time elapsed: ", time.time() - start_time, "s"

job_server.print_stats()


#####OVER############



#################
#Set_Master_NFS #
#################

cmd = "ssh root@" + Master_IP +" " + \
        "/sbin/chkconfig nfs on;" + \
        "/sbin/chkconfig nfslock on;" + \
        "/etc/init.d/nfslock restart;" + \
        "/etc/init.d/nfs restart"

print cmd
scp_child=pexpect.spawn(cmd)
index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+Master_IP+"'s password:", \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
if index == 0:
        scp_child.sendline('yes')
        ret = expectandshow(scp_child, ["root@"+Master_IP+"'s password:", pexpect.
EOF])
        if ret == 0:
                scp_child.sendline(password)
                expectandshow(scp_child, [pexpect.EOF])
        elif ret == 1:
                pass
elif index == 1:
        scp_child.sendline(password)
        expectandshow(scp_child, [pexpect.EOF])
elif index == 2:
        print "timeout, ignore and continue."
elif index == 3:
        pass

########Over	

###############
#Set_Salve_NFS
###############

start_time = time.time()
ppservers = ()
job_server = pp.Server(total_ip, ppservers=ppservers)
print "Starting pp with", job_server.get_ncpus(), "workers"
jobs1 = []
order1 = order[1:total_ip]
for ip in order1:
        jobs1.append(job_server.submit(set_salve_nfs, (ip,Master_IP,), (expectandshow,), ("pexpect",)))
for job in jobs1:
    job()
print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()

#####################

##################
###Set_Master_NIS#
##################

cmd="ssh root@"+Master_IP+ " " +\
        "/sbin/chkconfig ypserv of;" + \
        "/etc/init.d/ypserv start;" + \
        "/usr/lib64/yp/ypinit -m;" + \
        "/sbin/chkconfig ypbind on;" + \
        "/etc/init.d/ypbind start;"


print cmd
scp_child=pexpect.spawn(cmd)
index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+Master_IP+"'s password:", \
                                "next host to add:", \
                                "Is this correct?  [y/n: y]" , \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
if index == 0:
        scp_child.sendline('yes')
        ret = expectandshow(scp_child, ["root@"+Master_IP+"'s password:",pexpect.
EOF])
        if ret == 0:
                scp_child.sendline(password)
                ret1 = expectandshow(scp_child, ["next host to add:", pexpect.EOF])
                if ret1 == 0:
                        scp_child.sendcontrol('D')
                        ret2 = expectandshow(scp_child, ["Is this correct?  [y/n: y]",pexpect.EOF])
                        if ret2 == 0:
                                scp_child.sendline("y")
                                expectandshow(scp_child, [pexpect.EOF])
                        elif ret2 == 1:
                                pass
                elif ret1 == 1:
                        pass
        elif ret == 1:
                pass
elif index == 1:
        scp_child.sendline(password)
        ret1 = expectandshow(scp_child, ["next host to add:",pexpect.EOF])
        if ret1 == 0:
                scp_child.sendcontrol('D')
                ret2 = expectandshow(scp_child, ["Is this correct?  [y/n: y]",pexpect.EOF])
                if ret2 == 0:
                        scp_child.sendline("y")
                        expectandshow(scp_child, [pexpect.EOF])
                elif ret2 == 1:
                        pass
        elif ret1 == 1:
                pass

elif index == 2:
        print "timeout, ignore and continue."
elif index == 3:
        pass


#########OVER#########

######################
#######Set_Salve_Nis##
###################### 

start_time = time.time()
ppservers = ()
job_server = pp.Server(total_ip, ppservers=ppservers)
print "Starting pp with", job_server.get_ncpus(), "workers"
jobs2 = []
for ip in order1:
        jobs2.append(job_server.submit(set_salve_nis, (ip,), (expectandshow,), ("pexpect",)))

for job in jobs2:
    job()

print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()

#######OVER###########

#####################
####Set_All_Rsh######
#####################

start_time = time.time()
ppservers = ()
job_server = pp.Server(total_ip, ppservers=ppservers)
print "Starting pp with", job_server.get_ncpus(), "workers"
jobs3 = []
for ip in order:
        jobs3.append(job_server.submit(set_rsh, (ip,order,), (expectandshow,), ("pexpect",)))

for job in jobs3:
    job()

print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()

#########OVER#########

#####################
######Set_Mpd_Port###
#####################


cmd = "scp "+path+"/ip " + "root@"+Master_IP+":/root/mpd.hosts"

print cmd

scp_child=pexpect.spawn(cmd)
index=expectandshow(scp_child, \
        ['Are you sure you want to continue connecting', \
        "root@"+Master_IP+"'s password:", \
        pexpect.TIMEOUT, \
        pexpect.EOF])

cmd = "scp "+path+"/ip " + "root@"+Master_IP+":/root/mpd.hosts"

print cmd

scp_child=pexpect.spawn(cmd)
index=expectandshow(scp_child, \
        ['Are you sure you want to continue connecting', \
        "root@"+Master_IP+"'s password:", \
        pexpect.TIMEOUT, \
        pexpect.EOF])
if index == 0:
        scp_child.sendline('yes')
        ret = expectandshow(scp_child, ["root@"+Master_IP+"'s password:", pexpect.EOF])
        if ret == 0:
                scp_child.sendline(password)
                #scp_child.sendcontrol('c')
                expectandshow(scp_child, [pexpect.EOF])
        elif ret == 1:
                pass
elif index == 1:
                scp_child.sendline(password)
                #scp_child.sendcontrol('c')
                expectandshow(scp_child, [pexpect.EOF])
elif index == 2:
                print "timeout, ignore and continue."
elif index == 3:
                pass

cmd = "ssh root@" + Master_IP + " " + \
        "mpdallexit; mpdboot --rsh=rsh -n 4 --file=/root/mpd.hosts"

print cmd
scp_child=pexpect.spawn(cmd)
index=expectandshow(scp_child, \
                    ['Are you sure you want to continue connecting', \
                   "root@"+Master_IP+"'s password:", \
                   pexpect.TIMEOUT, \
                   pexpect.EOF])
if index == 0:
        scp_child.sendline('yes')
        ret = expectandshow(scp_child, ["root@"+Master_IP+"'s password:", pexpect.EOF])
        if ret == 0:
                scp_child.sendline(password)
                expectandshow(scp_child, [pexpect.EOF])
        elif ret == 1:
                pass
elif index == 1:
        scp_child.sendline(password)
        expectandshow(scp_child, [pexpect.EOF])
elif index == 2:
        print "timeout, ignore and continue."
elif index == 3:
        pass


print "Time elapsed: ", time.time() - start_time_total, "s"
